﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using JeffWilcox.Utilities.Silverlight;
using System.Windows.Browser;
using System.Threading;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Reflection;
using VkApi.DataTypes;

namespace VkApi
{
    public partial class VkApi
    {
        /// <summary>
        /// Sends notification to the users.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="uids">The user IDs.</param>
        /// <param name="message">The message of the notification.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of user IDs which got the notification.</returns>
        public long Secure_SendNotification(string d, int random, long[] uids, string message, ApiCallback<long[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));
            string uidsString = "";
            foreach (int s in uids)
                uidsString += s.ToString() + ",";
            uidsString = uidsString.Remove(uidsString.Length - 1);
            list.Add(new Parameter("uids", uidsString));
            list.Add(new Parameter("message", message));
            return this.SendRequest("secure.sendNotification", callback, list.ToArray());
        }

        /// <summary>
        /// Saves the current status of the application.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="uid">The user ID.</param>
        /// <param name="status">The status of the application.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success, else - error code.</returns>
        public long Secure_SaveAppStatus(string d, int random, long uid, string status, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));
            list.Add(new Parameter("uid", uid.ToString()));
            list.Add(new Parameter("status", status));
            return this.SendRequest("secure.saveAppStatus", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the status saved with SaveAppStatus. Available from the server-side only.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="uid">The user ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Status text.</returns>
        public long Secure_GetAppStatus(string d, int random, long uid, ApiCallback<string> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));
            list.Add(new Parameter("uid", uid.ToString()));        
            return this.SendRequest("secure.getAppStatus", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the balance.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Secure_GetAppBalance(string d, int random, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));          
            return this.SendRequest("secure.getAppBalance", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the balance of the user.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="uid">The user ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Secure_GetBalance(string d, int random, long uid, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));
            list.Add(new Parameter("uid", uid.ToString()));
            return this.SendRequest("secure.getBalance", callback, list.ToArray());
        }

        /// <summary>
        /// Withdraws the votes from users balance.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="uid">The user ID.</param>
        /// <param name="votes">The votes count.</param>
        /// <param name="test_mode">The test mode flag. If 1 then no votes will be withdrawed. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Count transferred votes.</returns>
        public long Secure_WithdrawVotes(string d, int random, int uid, int votes, int? test_mode, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));
            list.Add(new Parameter("uid", uid.ToString()));
            list.Add(new Parameter("votes", votes.ToString()));
            if (test_mode!=null) list.Add(new Parameter("test_mode", test_mode.ToString()));
            return this.SendRequest("secure.withdrawVotes", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the transactions history.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="type">The type of transactions. 0 - all, 1 - from user to app, 2 - from app to user, 3 - from user to user. 0 by default. (unnecessary)</param>
        /// <param name="uid_from">The user ID who send. (unnecessary)</param>
        /// <param name="uid_to">The user ID who recieved. (unnecessary)</param>
        /// <param name="date_from">The timestamp of transaction beginning. (unnecessary)</param>
        /// <param name="date_to">The timestemp of transaction ending. (unnecessary)</param>
        /// <param name="limit">The limit of result. 1000 by default. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Transactions.</returns>
        public long Secure_GetTransactionsHistory(string d, int random, int? type, long? uid_from, long? uid_to, string date_from, string date_to, long? limit, ApiCallback<Transaction[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));
            if (type != null) list.Add(new Parameter("type", type.ToString()));
            if (uid_from != null) list.Add(new Parameter("uid_from", uid_from.ToString()));
            if (uid_to != null) list.Add(new Parameter("uid_to", uid_to.ToString()));
            if (date_from != null) list.Add(new Parameter("date_from", date_from.ToString()));
            if (date_to != null) list.Add(new Parameter("date_to", date_to.ToString()));
            if (limit != null) list.Add(new Parameter("limit", limit.ToString()));
            return this.SendRequest("secure.getTransactionsHistory", callback, list.ToArray());
        }

        /// <summary>
        /// Adds the rating.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="uid">The user ID.</param>
        /// <param name="rate">The rate to add.</param>
        /// <param name="message">The message of adding. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Count of added rating if success.</returns>
        public long Secure_AddRating(string d, int random, long uid, int rate, string message, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));
            list.Add(new Parameter("uid", uid.ToString()));
            list.Add(new Parameter("rate", rate.ToString()));

            if (message != null)
                list.Add(new Parameter("message", message));

            return this.SendRequest("secure.addRating", callback, list.ToArray());
        }

        /// <summary>
        /// Sets the counter.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="uid">The user ID.</param>
        /// <param name="counter">The counter to set.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        public long Secure_SetCounter(string d, int random, long uid, int counter, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));
            list.Add(new Parameter("uid", uid.ToString()));
            list.Add(new Parameter("counter", counter.ToString()));           
            return this.SendRequest("secure.setCounter", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the SMS history.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="uid">The user ID. (unnecessary)</param>
        /// <param name="date_from">The date of beginning. (unnecessary)</param>
        /// <param name="date_to">The date of ending. (unnecessary)</param>
        /// <param name="limit">The limit of results. 1000 by default. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of SMS notifications.</returns>
        public long Secure_GetSMSHistory(string d, int random, long? uid, string date_from, string date_to, long? limit, ApiCallback<SMSNotification[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));
            if (uid != null) list.Add(new Parameter("uid", uid.ToString()));
            if (date_from != null) list.Add(new Parameter("date_from", date_from));
            if (date_to != null) list.Add(new Parameter("date_to", date_to));
            if (limit != null) list.Add(new Parameter("limit", limit.ToString()));
            return this.SendRequest("secure.getSMSHistory", callback, list.ToArray());
        }

        /// <summary>
        /// Sends the SMS notification.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="uid">The user ID.</param>
        /// <param name="message">The message of the notification.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success. Else - error code.</returns>
        public long Secure_SendSMSNotification(string d, int random, long uid, string message, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));
            list.Add(new Parameter("uid", uid.ToString()));
            list.Add(new Parameter("message", message));
            return this.SendRequest("secure.sendSMSNotification", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the SMS messages.
        /// </summary>
        /// <param name="d">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="uid">The user ID. (unnecessary)</param>
        /// <param name="date_from">The date of beginning. (unnecessary)</param>
        /// <param name="date_to">The date of ending. (unnecessary) </param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of SMS notifications. Max 100.</returns>
        public long Secure_GetSMS(string d, int random, long? uid, string date_from, string date_to, ApiCallback<SMSNotification[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("d", d));
            list.Add(new Parameter("title", random.ToString()));
            if (uid != null) list.Add(new Parameter("uid", uid.ToString()));
            if (date_from != null) list.Add(new Parameter("date_from", date_from));
            if (date_to != null) list.Add(new Parameter("date_to", date_to));            
            return this.SendRequest("secure.getSMS", callback, list.ToArray());
        }

        /// <summary>
        /// Sets language phrase for translation on other languages. Can be run on server-side only.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="timestamp">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="key">The unique key for this phrase.</param>
        /// <param name="value">The translated phrase.</param>
        /// <param name="locale">The local language. Can be ru or en. (unnecessary)</param>
        /// <param name="description">The description of translation. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        public long Secure_SetLanguageValue(long app_id, string sig, string v, string timestamp, int random, string key, string value, string locale, string description, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("timestamp", timestamp));
            list.Add(new Parameter("random", random.ToString()));
            list.Add(new Parameter("key", key));
            list.Add(new Parameter("value", value));
            if (locale != null) list.Add(new Parameter("locale", locale));
            if (description != null) list.Add(new Parameter("description", description));            
            return this.SendRequest("secure.setLanguageValue", callback, list.ToArray());
        }

        /// <summary>
        /// Deletes the phrase.
        /// </summary>
        /// <param name="app_id">The application ID.</param>
        /// <param name="sig">The signature. equals md5(viewer_idparam1=value1param2=value2secret)</param>
        /// <param name="v">The version of API. Current 2.0</param>
        /// <param name="timestamp">The UNIX-time of server.</param>
        /// <param name="random">The random number for request unification.</param>
        /// <param name="key">The unique key for this phrase.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        public long Secure_DeleteLanguageValue(long app_id, string sig, string v, string timestamp, int random, string key, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("app_id", app_id.ToString()));
            list.Add(new Parameter("sig", sig));
            list.Add(new Parameter("v", v));
            list.Add(new Parameter("timestamp", timestamp));
            list.Add(new Parameter("random", random.ToString()));
            list.Add(new Parameter("key", key));           
            return this.SendRequest("secure.deleteLanguageValue", callback, list.ToArray());
        }



    }
}
